<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input v-model="userInfo.password" type="password" style="width:300px" disabled />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSave">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { saveEmployeesBatch } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    // 加载用户信息
    async loadUserDetailById() {
      try {
        const { data: res } = await getUserDetailById(this.id)
        console.log('getUserDetailById', res)
        this.userInfo.username = res.username
        this.userInfo.password = res.password
      } catch (error) {
        console.log('getUserDetailById', error)
      }
    },
    // 修改用户的账号信息
    async updateEmployeesBatch() {
      try {
        const res = await saveEmployeesBatch(this.userInfo)
        console.log('saveEmployeesBatch', res)
        // 保存数据
      } catch (err) {
        console.log('saveEmployeesBatch', err)
      }
    },
    hSave() {
      this.$refs.userForm.validate(valid => {
        if (!valid) return
        this.updateEmployeesBatch()
      })
    }
  }
}
</script>

  <style>
  </style>

